<?php

namespace ticket\modules\backend\models;

use ticket\libs\Tools;
use Yii;

/**
 * This is the model class for table "tk_application".
 *
 * @property int $aid
 * @property int $app_id 实例id
 * @property string $name 应用名称
 * @property string $app_key
 * @property string $aes_key
 * @property string $aes_iv
 * @property string $remake
 * @property string $configuration
 * @property string $status 1-启用，2-禁用
 * @property string $create_time
 */
class Application extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_application';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['app_id', 'name','configuration'], 'required'],
            [['app_id'], 'integer'],
            [['status'], 'string'],
            [['create_time'], 'safe'],
            [['name', 'app_key'], 'string', 'max' => 32],
            [['aes_key', 'aes_iv'], 'string', 'max' => 16],
            [['remake'], 'string', 'max' => 255],
            [['configuration'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'aid' => 'Aid',
            'app_id' => 'App ID',
            'name' => 'Name',
            'app_key' => 'App Key',
            'aes_key' => 'Aes Key',
            'aes_iv' => 'Aes Iv',
            'remake' => 'Remake',
            'configuration' => 'Configuration',
            'status' => 'Status',
            'create_time' => 'Create Time',
        ];
    }

    /*** 添加应用
     * @param $app_id
     * @param $parmas
     * @return bool
     */
    public function addApplication($app_id,$parmas)
    {

        $data = $this->setData($parmas);
        $data['Application']['app_id'] = $app_id;
        $data['Application']['app_key'] = Tools::createStr(32);
        $data['Application']['aes_key'] = Tools::createStr(16);
        $data['Application']['aes_iv'] = Tools::createStr(16);
        $data['Application']['create_time'] = date("Y-m-d H:i:s");

        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }

    /*** 修改应用
     * @param $aid
     * @param $app_id
     * @param $params
     * @return bool
     */
    public function modApplication($aid,$app_id,$params)
    {
        $data = $this->setData($params);
        $data['Application']['app_id'] = $app_id;
        if ($this->load($data) and $this->validate()){
            $model = self::find()->where('aid = :id',[':id' => $aid])->one();
            if (!is_null($model)){
                $model->name = $data['Application']['name'];
                $model->remake = $data['Application']['remake'];
                $model->status = $data['Application']['status'];
                $model->configuration = $data['Application']['configuration'];
                if ($model->save(0)){
                    return true;
                }
            }
        }
        return false;
    }


    /*** 删除应用
     * @param $aid
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function delApplication($aid)
    {
        $model = self::find()->where('aid = :id',[':id' => $aid])->one();
        if (!is_null($model)){
            if ($model->delete()){
                return true;
            }
        }
        return false;
    }


    public function getApplicationInfo($aid)
    {
        $data = self::find()->where('aid = :id',[':id' => $aid])->asArray()->one();
        if (!empty($data['configuration'])){
            $data['configuration'] = json_decode($data['configuration'],true);
        }
        return $data;
    }



    

    public function getApplicationList($app_id)
    {
        $data = self::find()->where('app_id = :apid',[':apid' => $app_id])->select(['aid','name'])->asArray()->all();
        return $data;
    }


    private function setData($params)
    {
        if (isset($params['name']) and !empty($params['name'])){
            $data['Application']['name'] = $params['name'];
        }else{
            $data['Application']['name'] = '';
        }
        if (isset($params['remake']) and !empty($params['remake'])){
            $data['Application']['remake'] = $params['remake'];
        }else{
            $data['Application']['remake'] = '';
        }

        if (isset($params['configuration']) and !empty($params['configuration'])){
            $data['Application']['configuration'] = json_encode($params['configuration']);
        }else{
            $data['Application']['configuration'] = '';
        }
        if (isset($params['status']) and !empty($params['status'])){
            $data['Application']['status'] = $params['status'];
        }else{
            $data['Application']['status'] = '1';
        }
        return $data;

    }
}
